$(function(){
  function copyToClipboard(elem) {
          // create hidden text element, if it doesn't already exist
          var targetId = "_hiddenCopyText_";
          var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA";
          var origSelectionStart, origSelectionEnd;
          if (isInput) {
              // can just use the original source element for the selection and copy
              target = elem;
              origSelectionStart = elem.selectionStart;
              origSelectionEnd = elem.selectionEnd;
          } else {
              // must use a temporary form element for the selection and copy
              target = document.getElementById(targetId);
              if (!target) {
                  var target = document.createElement("textarea");
                  target.style.position = "absolute";
                  target.style.left = "-9999px";
                  target.style.top = "0";
                  target.id = targetId;
                  document.body.appendChild(target);
              }
              target.textContent = elem.textContent;
          }
          // select the content
          var currentFocus = document.activeElement;
          target.focus();
          target.setSelectionRange(0, target.value.length);

          // copy the selection
          var succeed;
          try {
              succeed = document.execCommand("copy");
          } catch(e) {
              succeed = false;
          }
          // restore original focus
          if (currentFocus && typeof currentFocus.focus === "function") {
              currentFocus.focus();
          }

          if (isInput) {
              // restore prior selection
              elem.setSelectionRange(origSelectionStart, origSelectionEnd);
          } else {
              // clear temporary content
              target.textContent = "";
          }
          return succeed;
      }
    $(document).on('click','.yjfz', function(event) {
             var my_index=$(this);
             var copyMark = copyToClipboard(my_index.siblings('span')[0]);
             if (copyMark) {alert("复制成功")};
      });

    $('.chose_box').click(function(event) {
        $('.right_box').addClass('right_box1');
        $('.layer').show();
    });
    $('.layer_box li').click(function(event) {
      var my_src=$(this).find('img').attr('src');
      var my_nei=$(this).find('p').text();
      var bankName = $(this).find('img').attr("name");
        $('.chose_box img').attr('name' ,  bankName);
        $("input[name='BankType']").attr('value' ,  bankName);
        $('.chose_box img').attr('alt' ,  my_nei);
        $('.layer').hide();
        $('.right_box').removeClass('right_box1');
        $('.chose_box img').attr('src',my_src);
        $('.chose_box span').text(my_nei);
    });
    $('.tianjia').click(function(event) {
        $('.layer2').show();
    });
    $('.btn_po').click(function(event) {
        $('.layer2').hide();
    });
    $('.guan').click(function(event) {
        $('.layer2').hide();
    });var btn = document.getElementById("getcode");
    //调用监听
    monitor($(btn));
    //点击click
    // btn.onclick = function() {
    //倒计时效果  getCode回调函数  获取验证码api
    countDown($(btn), getCode);
    // };
    function getCode() {
        console.log("倒计时开始");
    }


    //防止页面刷新倒计时失效
    /**
     *
     * @param {Object} obj  获取验证码按钮
     */
    function monitor(obj) {
        var LocalDelay = getLocalDelay();
        if(LocalDelay.time!=null){
            var timeLine = parseInt((new Date().getTime() - LocalDelay.time) / 1000);
            if (timeLine > LocalDelay.delay) {
                console.log("过期");
            } else {
                _delay = LocalDelay.delay - timeLine;
                obj.text(_delay+"s");
                document.getElementById("getcode").disabled = true;
                var timer = setInterval(function() {
                    if (_delay > 1) {
                        _delay--;
                        obj.text(_delay+"s");
                        setLocalDelay(_delay);
                    } else {
                        clearInterval(timer);
                        obj.text("有效倒计时");
                        document.getElementById("getcode").disabled = false;
                    }
                }, 1000);
            }
        }
    };
    //倒计时效果
    /**
     *
     * @param {Object} obj 获取验证码按钮
     * @param {Function} callback  获取验证码接口函数
     */
    function countDown(obj, callback) {
        if (obj.text() == "有效时间") {
            var _delay = 180;
            var delay = _delay;
            obj.text(_delay+"s");
            document.getElementById("getcode").disabled = true;
            var timer = setInterval(function() {
                if (delay > 1) {
                    delay--;
                    obj.html(delay+"s");
                    setLocalDelay(delay);
                } else {
                    clearInterval(timer);
                    obj.text("订单已失效，请重新发起支付");
                    document.getElementById("getcode").disabled = false;
                }
            }, 1000);

            callback();
        } else {
            return false;
        }
    }
    //设置setLocalDelay
    function setLocalDelay(delay) {
        //location.href作为页面的唯一标识，可能一个项目中会有很多页面需要获取验证码。
        sessionStorage.setItem("delay_" + location.href, delay);
        sessionStorage.setItem("time_" + location.href, new Date().getTime());
    }

    //getLocalDelay()
    function getLocalDelay() {
        var LocalDelay = {};
        LocalDelay.delay = sessionStorage.getItem("delay_" + location.href);
        LocalDelay.time = sessionStorage.getItem("time_" + location.href);
        return LocalDelay;
    }
})